package com.party.mobile.biz.member;

import com.google.common.collect.Lists;
import com.party.common.utils.DateUtils;
import com.party.core.model.sign.MemberSignDetail;
import com.party.core.service.sign.IMemberSignDetailService;
import com.party.mobile.web.dto.sign.output.SignRecodListOutput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: huangzequn
 * @Date: 2019/4/17 14:31
 */
@Service
public class MemberSignDetailBizService {

    @Autowired
    private IMemberSignDetailService memberSignDetailService;


    /**
    * @Author hzq
    * @Description //签到日历接口
    * @Date 15:21 2019/4/17
    * @param signId
    * @param yearMonth
    * @return
    **/
    public List<SignRecodListOutput> myCalendarSignList(String signId, String yearMonth) {
        Calendar newInstance = Calendar.getInstance();
        // 构造当前年份、月份的新日期
        String[] arr = yearMonth.split("-");
        newInstance.set(Calendar.YEAR, Integer.parseInt(arr[0]));
        newInstance.set(Calendar.MONTH, Integer.parseInt(arr[1]) - 1);
        newInstance.set(Calendar.DATE, 1);

        int monthLastDay = newInstance.getActualMaximum(Calendar.DAY_OF_MONTH); // 月最后一天
        newInstance.set(Calendar.DAY_OF_MONTH, 1);
        Date newStartTime = newInstance.getTime();
        newInstance.set(Calendar.DAY_OF_MONTH, monthLastDay);
        Date newEndTime = newInstance.getTime();

        List<SignRecodListOutput> monthList = Lists.newArrayList();

        // 开始日期和结束日期的中间日期
        List<Date> dateList = DateUtils.getDateDifference(newStartTime, newEndTime);
        for (Date day : dateList) {
            newInstance.setTime(day);
            int currentDay = newInstance.get(Calendar.DATE);
            // 补前面的空
            if (currentDay == 1) { // 每月1号
                int startDayOfWeek = newInstance.get(Calendar.DAY_OF_WEEK) - 1;
                for (int i = 0; i < startDayOfWeek; i++) { // 如果开始日期1号不是周日，补位默认日期
                    SignRecodListOutput output = new SignRecodListOutput();
                    output.setDay(0);
                    output.setSign(false);
                    monthList.add(output);
                }
            }

            // 正常部分
            // 正常部分
            SignRecodListOutput calendarDay = new SignRecodListOutput();
            calendarDay.setDay(currentDay);
            MemberSignDetail memberSignDetail = memberSignDetailService.getByDay(signId,day);
            if (memberSignDetail != null) {
                calendarDay.setSign(true);
            } else {
                calendarDay.setSign(false);
            }
            monthList.add(calendarDay);

            // 补后面的空
            if (currentDay == monthLastDay) { // 每月最后一日
                int endDayOfWeek = newInstance.get(Calendar.DAY_OF_WEEK) - 1;
                for (int i = endDayOfWeek; i < 6; i++) {
                    SignRecodListOutput output = new SignRecodListOutput();
                    output.setDay(0);
                    output.setSign(false);
                    monthList.add(output);
                }
            }
        }
        return monthList;
    }
}
